#!/bin/bash
set -e

export TZ=UTC
export HOME=/remote/iosg/home-2/bot-coin
source ~/miniconda3/etc/profile.d/conda.sh
conda activate coin2_env

cd ~/coin2_fastfeed/coin

machines=('feed-05.ap-northeast-1.aws' 'feed-05.cn-hongkong.aliyun' 'feed-02.ap-northeast-2.aws' 'feed-01.ap-northeast-1.aws.huobi' 'feed-02.us-east-1.aws' 'feed-02.eu-west-1.aws')

mkdir -p /remote/iosg/jenkins-2/bot-coin/jenkins/workspace/coin2_fastfeed/logs/$1/
for m in "${machines[@]}";
do
  mkdir -p /remote/iosg/jenkins-2/bot-coin/jenkins/workspace/coin2_fastfeed/logs/$1/$m
done

FuturesHuobiV1_groups=('a' 'b' 'c')
FuturesOkexV3_groups=('a' 'b' 'c' 'd' 'e' 'f')
SpotOkexV3_groups=('a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p')
FuturesOkexV3Swap_groups=('a')
SpotHuobiV1_groups=('a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'o' 'p')
FuturesBitmexV1_groups=('a')
SpotBinanceV1_groups=('a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p')
FuturesBinanceV1_groups=('a')
SpotGdaxV1_groups=('a' 'b' 'c' 'd' 'e')
SpotKrakenV1_groups=('a')
FuturesHuobiV1Swap_groups=('a')

declare -A mea_group
mea_group['Futures.Huobi.v1']=FuturesHuobiV1_groups[@]
mea_group['Futures.Okex.v3']=FuturesOkexV3_groups[@]
mea_group['Spot.Okex.v3']=SpotOkexV3_groups[@]
mea_group['Futures.Okex.v3-swap']=FuturesOkexV3Swap_groups[@]
mea_group['Spot.Huobi.v1']=SpotHuobiV1_groups[@]
mea_group['Futures.Bitmex.v1']=FuturesBitmexV1_groups[@]
mea_group['Spot.Binance.v1']=SpotBinanceV1_groups[@]
mea_group['Futures.Binance.v1']=FuturesBinanceV1_groups[@]
mea_group['Spot.Gdax.v1']=SpotGdaxV1_groups[@]
mea_group['Spot.Kraken.v1']=SpotKrakenV1_groups[@]
mea_group['Futures.Huobi.v1-swap']=FuturesHuobiV1Swap_groups[@]

FuturesHuobiV1_recipes=('snapshot' 'incremental')
FuturesOkexV3_recipes=('snapshot' 'realtime')
SpotOkexV3_recipes=('snapshot' 'realtime')
FuturesOkexV3Swap_recipes=('snapshot' 'realtime')
SpotHuobiV1_recipes=('snapshot')
FuturesBitmexV1_recipes=('realtime')
SpotBinanceV1_recipes=('snapshot' 'l1_realtime')
FuturesBinanceV1_recipes=('snapshot' 'realtime' 'l1_realtime')
SpotGdaxV1_recipes=('snapshot')
SpotKrakenV1_recipes=('snapshot')
FuturesHuobiV1Swap_recipes=('snapshot' 'incremental')

declare -A recipes
recipes['Futures.Huobi.v1']=FuturesHuobiV1_recipes[@]
recipes['Futures.Okex.v3']=FuturesOkexV3_recipes[@]
recipes['Spot.Okex.v3']=SpotOkexV3_recipes[@]
recipes['Futures.Okex.v3-swap']=FuturesOkexV3Swap_recipes[@]
recipes['Spot.Huobi.v1']=SpotHuobiV1_recipes[@]
recipes['Futures.Bitmex.v1']=FuturesBitmexV1_recipes[@]
recipes['Spot.Binance.v1']=SpotBinanceV1_recipes[@]
recipes['Futures.Binance.v1']=FuturesBinanceV1_recipes[@]
recipes['Spot.Gdax.v1']=SpotGdaxV1_recipes[@]
recipes['Spot.Kraken.v1']=SpotKrakenV1_recipes[@]
recipes['Futures.Huobi.v1-swap']=FuturesHuobiV1Swap_recipes[@]

params=()

for m in "${machines[@]}";
do
  for mea in "${!mea_group[@]}";
  do
    for recipe in "${!recipes[$mea]}";
    do
      for g in "${!mea_group[$mea]}";
      do
        params+=("$m" "$mea" "$g" "1" "$recipe")
        params+=("$m" "$mea" "$g" "2" "$recipe")
      done
    done
  done
done

echo $params
cmd="srun --job-name='coin2_fastfeed_$1_{1}_{2}_{5}_{3}_{4}' --nodes=1 --ntasks=1 catchsegv ../fastfeed-converter --driver data/coin2/feed/ops/fastfeed-converter/common.json --driver data/coin2/feed/ops/feed-writer/{1}.json --date $1 --mea {2} --group {3} --worker {4} --recipe {5}"
parallel --joblog fastfeed-gen.log --jobs 0 --max-args 5 $cmd ::: ${params[*]};
